#ifndef __COMM_HPP__
#define __COMM_HPP__
#include<iostream>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<cstring>
#include<sys/stat.h>
using namespace std;
#include"log.hpp"

Log log;

const char* pathname="/home/bqx";
const int proj_id=0x6666;
int size=4069;
//获取key值
key_t GetKey()
{
    key_t key=ftok(pathname,proj_id);
    if(key==-1)
    {
        log(Fatal,"ftok error:%s",strerror(errno));
        exit(1);
    }
    log(Info,"ftok success,key is :%d",key);
    return key;
}

//创建
int  GetShmMemHelper(int flag)
{
    key_t k=GetKey();
    int shmid=shmget(k,size,flag);
    if(shmid==-1)
    {
        log(Fatal,"shmget error: %s",strerror(errno));
        exit(1);
    }
}

int CreatShm()
{
    return GetShmMemHelper(IPC_CREAT|IPC_EXCL|0666);
}

int GetShm()
{
    return GetShmMemHelper(IPC_CREAT);
}

#define FIFO_FILE "./myfifo"
#define MODE 0664

enum 
{
    FIFO_CREAT_ERR=1,
    FIFO_DELETE_ERR,
    FIFO_OPEN_ERR
};

class Init
{
public:
    Init()
    {
        //创建管道
        int n=mkfifo(FIFO_FILE,MODE);
        if(n==-1)
        {
            log(Fatal,"mkfifo err:%s",strerror(errno));
            exit(1);
        }
    }
    ~Init()
    {
        int m=unlink(FIFO_FILE);
        if(m<0)
        {
            log(Fatal,"unlink:%s",strerror(errno));
            exit(1);
        }
    }
};



#endif
